// 全屏
export function fullScreen () {
  let el = document.documentElement;
  let rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen;

  if (rfs) {
    rfs.call(el);
  } else if (typeof window.ActiveXObject !== "undefined") {
    //for IE，这里其实就是模拟了按下键盘的F11，使浏览器全屏
    let wscript = new ActiveXObject("WScript.Shell");
    if (wscript != null) {
      wscript.SendKeys("{F11}");
    }
  }
}

//退出全屏
export function exitScreen () {
  let el = document;
  let cfs = el.cancelFullScreen || el.webkitCancelFullScreen || el.mozCancelFullScreen || el.exitFullScreen;

  if (cfs) {
    cfs.call(el);
  }
  else if (typeof window.ActiveXObject !== "undefined") {
    //for IE，这里和fullScreen相同，模拟按下F11键退出全屏
    let wscript = new ActiveXObject("WScript.Shell");
    if (wscript != null) {
      wscript.SendKeys("{F11}");
    }
  }
}
export function debounce (func, wait) {
  let timeout;
  return function () {
    const context = this;
    const args = [...arguments];
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(() => {
      func.apply(context, args)
    }, wait);
  }
}
export function throttle (func, wait) {
  var previous = 0;
  return function () {
    let now = Date.now();
    let context = this;
    let args = arguments;
    if (now - previous > wait) {
      func.apply(context, args);
      previous = now;
    }
  }
}
export function formatTime (date) {
  if (!date) return ''
  date = date.toString()
  const d = new Date(date)
  const month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1)
  const day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
  const hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours()
  const min = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()
  const sec = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds()
  const times = d.getFullYear() + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + sec
  return times
}

// 定义动态路由
const r = require.context('../view', true, /\.vue$/)

export function initDynamicRoutes (Menu, arr) {
  Menu.forEach((item, index) => {
    try {
      const name = item.path.split('/')
      const lastPath = item.path.split('/').pop()

      if (item.redirect) {
        if (name.length > 2) {
          arr.push({
            children: [],
            redirect: item.redirect,
            meta: { title: item.title },
            path: lastPath,
            component: r('./Home/Home.vue').default,
          })

        } else if (item.name === "DataVisual" || item.name === "Visual_Platform" || item.path === "RootsShow") {
          arr.push({
            children: [],
            meta: { title: item.title },
            path: item.path,
            component: r('.' + item.redirect + '.vue').default,
          })

      } else {
        arr.push({
          children: [],
          redirect: item.redirect,
          meta: { title: item.title },
          path: item.path,
          component: r('./Home/Home.vue').default,
        })
      }

      } else {
        if (item.name === 'RootsShow') {
          arr.push({
            children: [],
            meta: { title: item.title },
            path: item.path,
            component: r('.' + item.component + '.vue').default,
          })
        } else {
          arr.push({
            children: [],
            component: r('.' + item.path + '.vue').default,
            meta: { title: item.title },
            path: lastPath,
          })
        }
      }

      if (item.childPermission.length) {
        const childArr = initDynamicRoutes(item.childPermission, [])
        arr[index].children = childArr
      }
    } catch( error ) {
      console.error( "加载路由出现错误:", error)
    }
  })
  // console.log('最后',arr)
  return arr
}

// 路由懒加载 由于版本是 webpack 3 无法进行 import 路由懒加载
// export function load(path) {
//   return () => import(/* webpackChunkName: "[request]" */ `@/view${path}`)
// }

// 本项目利用异步组件的形式实现路由懒加载
export function load (path) {
  return resolve => require([`@/view${path}`], resolve)
}


export function dateShorter (target, key) {
  target.forEach(item => {
    if (item[key])
      item[key] = item[key].slice(0, 10)

  }
  )
}